package tst.mng;

import java.sql.SQLException;
import java.util.Iterator;

import org.dom4j.DocumentException;

import indi.zjh.dp.db.Session;
import indi.zjh.dp.error.RelationClassNotMatchException;
import indi.zjh.dp.error.UnsuportedFieldException;
import indi.zjh.dp.orm.Business;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Relation;
import indi.zjh.dp.type.String256;

public class Log extends Business {
	protected String256 content = new String256();
	
	protected Relation node = new Relation(LogNode.class);
	
	public String getContent() {
		return content.string;
	}
	public void setContent(String content) {
		this.content.string = content;
	}
	public Relation getNode() {
		return node;
	}
	@Override
	public String toString() {
		return "Log [content=" + content + ", node=" + node + ", _id=" + _id + ", _v=" + _v + "]";
	}

	public static void info(Session session, String content, LogNode node) throws UnsuportedFieldException, DocumentException, Exception
	{
		Log log = new Log();
		log.setContent(content);
		log.getNode().add(node);
		session.save(log).commit();
	}
	
	public static void printLog(Session session, LogNode node, int limit) throws SQLException, DocumentException, UnsuportedFieldException, Exception
	{
		Query<Log> query = new Query<Log>(session);
		Iterator<Log> iter = query.find(Log.class).association(Log.class, LogNode.class).extend(LogNode.class, node.getClass()).order(Log.class, "`_created` DESC").limit(0, limit).result().iterator();
		while(iter.hasNext())
		{
			Log log = iter.next();
			System.out.println(log.toString()+","+node.toString());
		}
	}
	
}
